package com.cote.douhahou.pretloan.launcher;

import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import com.cote.douhahou.pretloan.any.timezone.AkomapalendTimeZoneUtil;
import com.cote.douhahou.pretloan.any.util.AkomapalendAnalyticsUtil;
import com.cote.douhahou.pretloan.beans.LogAkomapalendinInfo;
import com.cote.douhahou.pretloan.home.HomeActivity;
import com.cote.douhahou.pretloan.network.HttpHelper;
import com.cote.douhahou.pretloan.utils.AkomapaLendConfigs;
import com.cote.douhahou.pretloan.utils.AkomapaLendUtil;
import com.cote.douhahou.pretloan.R;

import org.json.JSONException;
import org.json.JSONObject;


public class LauncherAkomapalendActivity extends AppCompatActivity {

    private boolean firstFlag = true;

    private ActivityResultLauncher<Intent> requestPermissionLauncher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

        requestPermissionLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), this::permissionFinish);

        findViewById(R.id.al_start).setOnClickListener(v -> process());

    }

    @Override
    protected void onResume() {
        super.onResume();
        HttpHelper.inst().submitEvent("LAUNCH","START",getString(R.string.local_config));
        if (firstFlag) process();
    }

    @Override
    protected void onPause() {
        super.onPause();
        HttpHelper.inst().submitEvent("LAUNCH","END","");
    }

    private void permissionFinish(ActivityResult result){
        if (result.getResultCode()==RESULT_OK) handlerTimezone();
        else if (result.getResultCode() == 82) finish();
    }

    private void process(){
        HttpHelper.inst().fCountryConfig(this, AkomapalendTimeZoneUtil.INSTANCE.timeZoneID(),this::handlerTimezone);
    }

    public void handlerTimezone(){
        if(!AkomapaLendUtil.phonestatePermissionHasAgree(this) || (firstFlag && !AkomapaLendUtil.smsPermissionHasAgree(this))){
            firstFlag = false;
            requestPermissionLauncher.launch(new Intent(this, PermissionRequestActivity.class));
            return;
        }

        AkomapalendAnalyticsUtil.INSTANCE.init(BaseContext.instance());

        HttpHelper.inst().fetchAppConfig(this,this::handleAppConfig);
    }

    public void handleAppConfig(){

        if (AkomapaLendUtil.hasLogin()){
            try {
                JSONObject object = new JSONObject();
                object.put("token", AkomapaLendUtil.token());
                object.put("userId", AkomapaLendUtil.userId());
                object.put("type", AkomapaLendConfigs.LoginTypeOld);
                object.put("isAdSource", AkomapaLendUtil.adSource() ? "YES":"NO");
                LogAkomapalendinInfo.par_akomapalendse(object);

                HttpHelper.inst().process(this,this::handleProcess);
                return;
            }catch (JSONException e){
                e.printStackTrace();
            }
        }

        Intent intent = new Intent(this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
        finish();
    }

    public void handleProcess(){
        startActivity(new Intent(this, HomeActivity.class));
        finish();
    }

}